/*
 * Created Date: Tuesday November 20th 2018 10:42:38 am
 * Author: xionghaizi
 * -----
 * Last Modified:Tuesday November 20th 2018 10:42:38 am
 * Modified By: xionghaizi
 * -----
 * Copyright (c) 2018 <<company>>
 */
var host = env.host; // 数据接口
var title = env.title; 
var menu = services.menu;
var menuList = function(res) {

  if (res.data && res.data.length > 0) {
    $.each(res.data, function(i, v) {
      if (v.children && v.children.length > 0) {
        var liChildren=""
        $.each(v.children, function(index, value){
          liChildren += "<li class='itangquan-menu-title' style='padding-left: 48px' data-url='"+'./page/'+value.url+'/index.html'+"'>"+ value.name +"</li>"
        })
         var li = "<li>"+
            "<div class='itangquan-menu-title' data-open='isOpen'>"+
              "<span class='glyphicon glyphicon-th-large' aria-hidden='true'></span>"+
              "<span>"+v.name+"</span>"+
              "<span class='glyphicon glyphicon-menu-down' aria-hidden='true'></span>"+
            "</div>"+
            "<ul class='itangquan-menu-sub menu-sub-hidden'>"+
              liChildren +
            "</ul>"+
          "</li>";
        $(".itangquan-menu").append(li);
      } else {
        $(".itangquan-menu").append("<li class='itangquan-menu-title' data-url='" + './page/' + v.url + '/index.html' +"'>" +
          "<span class='glyphicon glyphicon-th-large' aria-hidden='true'></span>" +
          "<span>"+ v.name + "</span>" +
          "<span class='glyphicon' aria-hidden='true'></span>"+
          "</li>"
        )
      }
    })
  }
  menuEventOnload(); // 载入动态dom相关菜单事件
 $(".itangquan-menu-title:eq(0)").click();
}
iTangQuanAjax(host + menu, "GET", {}, menuList);

/**
* @description menuEventOnload 菜单相关事件
 * @author 
 * @date 2018-11-21
 */
function menuEventOnload() {
  /**
 * @description 导航hover事件
 * @author 
 * @date 2018-11-20
 */
  // $(".itangquan-menu-title").each(function () {
  //   $(this).hover(function () {
  //     $(this).addClass("menu-title-active");
  //   }, function () {
  //     $(this).removeClass("menu-title-active")
  //   })
  // })
  /**
   * @description 二级导航显示与隐藏
   * @date 2018-11-20
   */
  $("div[data-open='isOpen']").click(function () {
    var type = "nextSibling";
    var ulElement = $(this[type]);
    var menuSubHidden = ulElement.attr("class").split(" ").toString().indexOf("menu-sub-hidden");
    if (menuSubHidden > 0) {
      $("div[data-open='isOpen']").each(function(i,element) {
        // var elementNum = $(element).next().attr("class").split(" ").indexOf("menu-sub-hidden");
        var elementNum = $(element[type]).attr("class").split(" ").toString().indexOf("menu-sub-hidden");
        console.log(elementNum, "ggg")
        if (elementNum < 0) {
          $(element).children("span:last-child").removeClass("glyphicon-menu-up").addClass("glyphicon-menu-down");
          $(element).next().addClass("menu-sub-hidden");
        }
      })
      ulElement.removeClass("menu-sub-hidden").show();
      $(this).children("span:last-child").removeClass("glyphicon-menu-down").addClass("glyphicon-menu-up")
    } else {
      ulElement.addClass("menu-sub-hidden");
      $(this).children("span:last-child").removeClass("glyphicon-menu-up").addClass("glyphicon-menu-down");
    }
  })
  /**
   * @description 导航选中效果
   * @date 2018-11-20
   */
  $(".itangquan-menu-title").click(function () {
    var isLi = $(this).is("li");
    if (isLi) {
      $(".itangquan-menu-title").removeClass("menu-selected");
      $(this).addClass("menu-selected");
      $(".itangquan-layout-body .iframe-body > iframe").attr("src", $(this).attr("data-url"))
    }
  })
  $("title").html(title)
}


